home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
EnigmA Amiga Run 1997 May
/
EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso
/
softwareupdate
/
system
/
amigados
/
filefunctions
/
filefunctions.doc
< prev
next >
Wrap
Text File
|
1996-10-10
|
18KB
|
550 lines
3 FILE FUNCTIONS
3.1 INTRODUCTION
In this chapter I will describe some useful file functions you
might need when you are working with AmigaDOS:
1. Create directories
2. Delete files and directories
3. Rename files and directories
4. Rename volumes
5. Attach comments to files and directories
6. Alter the "protection bits" on files and directories
3.2 CREATE DIRECTORIES
To create directories you use the function "CreateDir()". You
only need to give the function a string with the name of the
new directory (including any necessary path), and the directory
will automatically be created. If the directory could
successfully be created an exclusive lock to the new directory
is returned, else if there was an error and the directory could
not be created NULL is returned.
Since the directory is automatically locked if it was created
you have to unlock it yourself as soon as you do not need the
lock any more.
If there already exist a directory with the same name the
function will fail.
---------------------------------------------------------------
CreateDir()
ROM library: "dos.library/CreateDir", (All versions)
#include <clib/dos_protos.h>
Creates a directory and puts an exclusive lock on it.
Synopsis: lock = CreateDir( dir_name );
lock: (BPTR) If the directory could be created or there
already exist a direcoty with the specified name
a BPTR (BPCL pointer) is returned. If the directory
on the other hand could not be created NULL is
returned.
dir_name: (STRPTR) Pointer to a text string which contains
the name of the new directory (including any
necessary path).
If you have successfully created a directory you must unlock it
as soon as you do not need the lock any more!
Here is a simple example on how to use the CreateDir()
function:
/* A "BCPL" pointer to our directory lock: */
BPTR my_dir_lock;
- - -
/* Try to create a directory: */
my_dir_lock = CreateDir( "Ram:MyDirectory" );
/* Check if we have successfully created */
/* the directory or not: */
if( my_dir_lock == NULL )
{
/* Problems! */
printf( "Could not create the directory!\n" );
exit( 20 );
}
- - -
/* When we do not need the directory lock */
/* any more we unlock it: */
UnLock( my_dir_lock );
See also: Lock(), UnLock()
---------------------------------------------------------------
3.3 DELETE FILES AND DIRECTORIES
When you want to delete a file or directory you should use the
"DeleteFile()" function. You only need to give the function a
string with the name of the file or directory you want to delte
(including any necessary path). If the file/directory could be
deleted TRUE is returned, else FALSE is returned (file could
not be deleted).
Note that you can only delete a directory which is empty.
---------------------------------------------------------------
DeleteFile()
ROM library: "dos.library/DeleteFile", (All versions)
#include <clib/dos_protos.h>
Deletes a specified file or directory.
Synopsis: ok = DeleteFile( name );
ok: (LONG) If the file/directory could be deleted TRUE is
returned, else FALSE is returned (the file/directory
could not be deleted).
name: (STRPTR) Pointer to a text string which contains
the name (including any necessary path) of the file
or directory you want to delete.
Remember that you can only delete empty directories.
Here is a simple example on how to use the DeleteFile()
function:
/* A simple boolean variable: */
BOOL ok;
/* Delete a file: */
ok = DeleteFile( "RAM:Unimportand.dat" );
/* Could we delete the file? */
if( ok )
printf( "File deleted!\n" );
else
printf( "File could not be deleted!\n" );
See also: Open(), CreateDir()
---------------------------------------------------------------
3.3 RENAME FILES AND DIRECTORIES
When you want to rename a file or directory you should use the
"Rename()" function. You can even move a file or directory
to other directories on a volume by simply renaming the path
accordingly. For example:
/* Move a file inside a volume: OK! */
Rename( "df0:Documents/Sale.doc", "df0:Letters/Sale.doc" );
We have now moved the file "Sale.doc" from the directory
"Documents" to directory "Letters". Note! You can not rename a
file from one volume to another, you can only move the file or
directory inside the volume. Here is an example that will not
work:
/* Try to move a file to another volume: INCORRECT! */
Rename( "df0:Documents/Sale.doc", "df1:Letters/Sale.doc" );
If you move a directory all objects (files and subdirectories)
will also be moved accordingly.
---------------------------------------------------------------
Rename()
ROM library: "dos.library/Rename", (All versions)
#include <clib/dos_protos.h>
Renames a specified file or directory. It is possible to rename
a file or directory so it is moved to another location on the
same volume.
Synopsis: ok = Rename( old_name, new_name );
ok: (LONG) If the file/directory could be renamed TRUE
is returned, else FALSE is returned (the file/
directory could not be renamed).
old_name: (STRPTR) Pointer to a text string which contains
the name (including any necessary path) of the file
or directory you want to rename.
new_name: (STRPTR) Pointer to a text string which contains
the new name (including any necessary path) of the
file or directory. The path may be different as
long as it is still on the same volume.
Note, you can not rename a file or directory to another volume!
You can not rename a volume with this function. You must then
instead use the new "Relabel()" function.
Here is a simple example on how to use the Rename() function:
/* A simple boolean variable: */
BOOL ok;
/* Rename a file: (We not only rename the actual file, but */
/* we also move the file inside a directory called "MyDir") */
ok = Rename( "RAM:HorribleName.doc", "RAM:MyDir/Good.doc" );
/* Could we rename the file? */
if( ok )
printf( "File renamed!\n" );
else
printf( "File could not be renamed!\n" );
See also: Relabel()
---------------------------------------------------------------
3.4 RENAME VOLUMES (RELABEL)
When you want to rename a volume you can not use the previously
discussed "Rename()" function. Instead you have to use a
function called "Relabel()" which was introduced with Release
2. Before you may use this function you must therefore make
sure that the user really has dos version 36 or higher!
---------------------------------------------------------------
Relabel()
ROM library: "dos.library/Relabel", (V36+)
#include <clib/dos_protos.h>
Renames a specified volume.
Synopsis: ok = Relabel( old_name, new_name );
ok: (LONG) If the volume could be renamed TRUE is
returned, else FALSE is returned (the volume could
not be renamed).
old_name: (STRPTR) Pointer to a text string which contains
the name of the volume you want to rename. Since
this is the current volume name you have to include
the colons ":" at the end of the volume name.
new_name: (STRPTR) Pointer to a text string which contains
the new name of the volume. There should NOT be any
colons (:) at the end of this name since it is for
the moment not a volume name (but it will hopefully
soon be).
Note that this function was first included in dos library V36!
You can only rename volumes with this function. To rename a
file or directory you have to use the "Rename()" function.
Here is a simple example on how to use the Relabel() function:
/* A simple boolean variable: */
BOOL ok;
/* Rename the device: (We will try to rename the volume */
/* "Programs:" to "SourceCode".) */
ok = Relabel( "Programs:", "SourceCode" );
/* Could we rename the volume? */
if( ok )
printf( "The volume was successfully renamed!\n" );
else
printf( "The volume could not be renamed!\n" );
See also: Rename()
---------------------------------------------------------------
3.5 ATTACH COMMENTS TO FILES AND DIRECTORIES
AmigaDOS allows you to set a comment on a file or directory.
The comment can give the user a brief description of what the
file contains, or who created it etc... This comment is very
handy to use when you want to give some extra information about
the file, and since you can connect a comment which is up to 80
characters long even rather long descriptions can be used.
The user can display a comment on a file or directory by using
the Shell (CLI) command "List":
1.Prog:>
1.Prog:> List df0:SourceCodes
Directory "df0:SourceCodest" on Monday 15-Mar-93
Miniblast.c 34155 ----rwed 12-Jan-93 23:35:20
: This version can be used on all Amigas! Still a bug on line 365!!! -AB-
Macroblast.c 299128 ----rwed 16-Jan-93 03:35:12
: Connects fine, prob. with unknown PubScreen, otherwise OK! -AB-
LockSystem.c 24899 ----rwed 04-Feb-93 01:45:34
: Has to be updated before use! Line 512 & 525, ok code, -AB-
and so on...
To set a comment on a file or directory you should use the
"SetComment()" function.
---------------------------------------------------------------
SetComment()
ROM library: "dos.library/SetComment", (All versions)
#include <clib/dos_protos.h>
Adds a comment to a file or direcotry.
Synopsis: ok = SetComment( file_name, comment );
ok: (LONG) If the comment could be attached to the file
or directory TRUE is returned, else FALSE is
returned (the comment could not be attached).
name: (STRPTR) Pointer to a text string which contains
the name (including any necessary path) of the file
or directory you want to add the comment to.
comment: (STRPTR) Pointer to a text string which contains
the comment you want to add. The comment can be up
to 80 characters long. (Any old comment will be
replaced.)
Here is a simple example on how to use the SetComment()
function:
/* A simple boolean variable: */
BOOL ok;
/* Add a comment to the file: */
ok = SetComment( "Ram:Hello.c", "My wonderful program!" );
/* Was the comment successfully added? */
if( ok )
printf( "The comment was successfully attached!\n" );
else
printf( "Error! Could not add the comment!\n" );
See also:
---------------------------------------------------------------
3.6 ALTER THE PROTECTION FLAGS
All files and directories have a field of bits which is called
"the protection bits". The bits can be turned on or off and it
will affect other programs that may use the files. The most
commonly used bit is bit 0 which tells AmigaDOS if the file or
directory may be deleted or not. This is the "delete" bit, and
since it is so commonly used the whole collection of bits has
been called "the protection bits" although the other bits have
nothing to do with protecting the file from being deleted.
The user can alter the bits with help of the Shell (CLI)
command "Protect". Here is the list of protection flags the
user can use (each flag will alter its corresponding bit):
Flag Description
-------------------------------------------------------------
s The file is a "script" and contains Shell commands. If
the user enter the name of a file with this flag set
will the file automatically be executed as a script. The
user do not have to add the Shell command "Execute" in
front of the file name.
p The file is "pure" and can therefore be made "resident".
If this falg is not set the Shell command "Resident"
will fail.
a The file has been archived. Everytime a file has been
modified this flag is removed. The flag is usually used
by backup programs which set this flag each time the
file has been archived.
r The file can be read by other programs. If this flag is
not set the file can not be read.
w The file can be altered by other programs. If this flag
is not set the file can not be altered.
e The file is "executable" (a normal program). If this
flag is not set Shell (CLI) will refuse to start the
file as a program.
d The file/directory can be deleted. If this flag is not
set the file can not be deleted.
Most of these flags are only useful for files, but it is
possible to use all of then on directories too even if most of
them will not have any effect on the directory.
Here is an exapmple on how the user can protect a file from
being accidentally deleted:
1.Prog:>
1.Prog:> Protect df0:Important.dat SUB d
To see which flags are set or not the user can call the Shell
(CLI) command "List":
1.Prog:>
1.Prog:> List df0:
Directory "df0:" on Monday 15-Mar-93
Important.dat 128722 ----rwe- 21-Feb-93 02:12:18
NotSoImportant.dat 103677 ----rwed 21-Feb-93 02:12:25
and so on...
As you can see has the "d" flag been removed, and the file can
then not be deletet. The only way to delete the file is to add
the "d" flag again and then delete the file.
If the flag is used by AmigaDOS depends on which version the
user has:
-------------------------------------------------------------
s Used by the Shell & CLI (WB 1.3 or higher). If the flag
is set the file will be executed as a script. (You do
no need to use the Shell command "Execute".)
p Used by Shell's (CLI's) "Resident" command (WB 1.3 or
higher). If the flag is set the file (program) can be
made "resident".
a Used by all AmigaDOS versions and the FFS. Every time
the file has been altered this flag is removed.
r Used by AmigaDOS V36 or higher and by the FFS. If it is
not set the file can not be read.
w Used by AmigaDOS V36 or higher and by the FFS. If it is
not set the file can not be altered.
e Used by the Shell & CLI (all AmigaDOS versions and the
FFS), if the flag is not set Shell and CLI refuses to
start it.
d Used by all AmigaDOS versions and the FFS. If it is not
set the file can not be deleted.
-------------------------------------------------------------
To alter the protection bits from C you should use the function
"SetProtection()".
---------------------------------------------------------------
SetProtection()
ROM library: "dos.library/SetProtection", (All versions)
#include <clib/dos_protos.h>
Alters the protection bits on a file or directory.
Synopsis: ok = SetComment( file_name, flags );
ok: (LONG) If the protection bits could be altered as
instructed the function returns TRUE, else FALSE is
returned (the protection bits could not be altered).
name: (STRPTR) Pointer to a text string which contains
the name (including any necessary path) of the file
or directory which protection bits you want to
alter.
flags: (LONG) The protection bits you want to alter. You
set and/oor removes the bits with help of the
following flags: (defined in header file
"dos/dos.h")
FIBF_DELETE:
If this flag is set the "d" bit is removed so
the file or directory can not be deleted. If
this flag is not set the "d" bit will be
added.
FIBF_EXECUTE:
If this flag is set the "e" bit is removed so
Shell (CLI) will refuse to start the file as a
program. If this flag is not set the "e" bit
will be added.
FIBF_WRITE:
If this flag is set the "w" bit is removed so
no one can alter the file. If this flag is not
set the "w" bit will be added.
FIBF_READ:
If this flag is set the "r" bit is removed so
no one can read the file. If this flag is not
set the "r" bit will be added.
FIBF_ARCHIVE:
If this flag is set the "a" bit will be added
which indicates that the file has been
archived. If this flag is not set the "a" bit
will be removed.
FIBF_PURE:
If this flag is set the "p" bit will be added
which tells the Shell command "Resident" that
this file is "pure" and can be made resident.
If this flag is not set the "p" bit will be
removed.
FIBF_SCRIPT:
If this flag is set the "s" bit will be added
which tells Shell that this file is a script.
If this flag is not set the "s" bit will be
removed.
Note that the first four flags will remove the
corresponding bits if the flags are set. The last
three flags will on the other hand set the
corresponding bits if the flags are set!
To set sveral flags simply add a "|" sign between
them. So to protect a file from being accidentally
deleted and to instruct the Shell that it is a
script you set this field to:
FIBF_DELETE | FIBF_SCRIPT
Note that the "e", "w" and "r" bits will also
be set and "a" and "p" removed!
Here is a simple example on how to use the SetProtection()
function:
/* A simple boolean variable: */
BOOL ok;
/* Protect the file from being accidentally deleted */
/* and tell Shell it is a script: */
ok = SetProtection( "S:PrepCompiler",
FIBF_DELETE | FIBF_SCRIPT );
/* Could we alter the protection bits as desired? */
if( ok )
printf( "The protection bits were successfully altered!\n" );
else
printf( "Error! Could not alter the protection bits!\n" );
See also:
---------------------------------------------------------------